package com.xoms.front.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xoms.front.controller.res.BaseResult;
import com.xoms.front.controller.res.ListResult;
import com.xoms.front.controller.res.ObjectResult;
import com.xoms.front.controller.res.bean.ROrderBean;
import com.xoms.front.controller.res.bean.ROrderListBean;
import com.xoms.front.controller.springmvc.req.GetOrderListParam;
import com.xoms.front.controller.springmvc.req.GetPutOrderParam;
import com.xoms.front.model.AccountBean;
import com.xoms.front.service.OrderService;
import com.xoms.front.service.exception.ServiceException;
import com.xoms.front.service.res.PageResult;
import com.xoms.util.NumUtil;

/**
 * 订单相关接口
 * 
 * @author stallmandu
 *
 */
@Component
public class OrderController {
	private static Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	OrderService orderServiceImpl;

	/**
	 * 卖品下单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<ROrderBean> putProOrder(GetPutOrderParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<ROrderBean> result = new ObjectResult<ROrderBean>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			List<PutOrderProJsonBean> pros = JSON.parseArray(param.getPros(), PutOrderProJsonBean.class);
			double actualMoeny = Math.abs(NumUtil.toDouble(param.getActualMoeny(), 0));
			int payType = NumUtil.toInt(param.getPayType(), 0);
			int isHand = NumUtil.toInt(param.getIsHand(), 0);
			String roomId = param.getRoomId(); // 挂帐的影厅id，如果选择挂帐，则不能为空
			int isAuth = NumUtil.toInt(param.getIsAuth(), 0); // 是否授权优惠 0未授权，1授权
			double authMoney = Math.abs(NumUtil.toDouble(param.getAuthMoney(), 0)); // 优惠金额
			String authAccId = param.getAuthAccId(); // 优惠人id
			String vipCardId = param.getVipCardId(); // 会员卡id
			String deviceMac = param.getDeviceMac(); // 设备mac地址
			// 卖品下单
			ROrderBean resData = orderServiceImpl.putProOrder(account, pros, actualMoeny, payType, isHand, roomId,
					isAuth, authMoney, authAccId, vipCardId, deviceMac);
			result.setFlag(resData != null);
			result.setData(resData);
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setFlag(false);
		}
		return result;
	}

	/**
	 * 订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<ROrderListBean> orderList(GetOrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<ROrderListBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取订单列表
			PageResult<ROrderListBean> resData = orderServiceImpl.orderList(account, page, PageSize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setFlag(false);
		}
		return result;
	}

}
